//
// Created by RandBII on 2020/12/27.
//

#ifndef FFMPEGFORANDROID_AUDIOGLRENDER_H
#define FFMPEGFORANDROID_AUDIOGLRENDER_H

#include "BaseGLRender.h"
#include "AudioRender.h"
#include "AudioFrame.h"
#include <thread>
#include <GLES3/gl3.h>
#include <detail/type_mat.hpp>
#include <detail/type_mat4x4.hpp>
#include <vec3.hpp>
#include <vec2.hpp>
#include <GLHelper.h>
#include <gtc/matrix_transform.inl>

#define RESAMPLE_LEVEL 40
#define MAX_AUDIO_LEVEL 5000

class AudioGLRender : public BaseGLRender {

public:
    static AudioGLRender *GetInstance();

    static void ReleaseInstance();

    virtual void OnSurfaceCreated() override;

    virtual void OnSurfaceChanged(int width, int height) override;

    virtual void UpdateMVPMatrix(int angelX, int angelY, int scaleX, int scaleY) override;

    virtual void SetTouchLoc(float touchX, float touchY) override;

    virtual void OnDrawFrame() override;

    void UpdateAudioFrame(AudioFrame *audioFrame);

private:
    void Init();

    void UnInit();

    AudioGLRender();

    ~AudioGLRender();

    void UpdateMesh();

// TODO 有疑问
    static AudioGLRender *mPInstance;
    static std::mutex mMutex;
    AudioFrame *mPAudioFrameBuffer = nullptr;

    GLuint mProgramObj;
    GLuint mVaoId;
    GLuint mVboIds[2];
    glm::mat4 mMVPMatrix;

    glm::vec3 *mVerticesCoords = nullptr;
    glm::vec2 *mTextureCoords = nullptr;

    int mRenderDataSize;

};


#endif //FFMPEGFORANDROID_AUDIOGLRENDER_H
